package com.ciwong.epaper.modules.pigai.ui;

import android.content.Intent;
import android.util.Log;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.application.EpaperConstant;
import com.ciwong.epaper.modules.cordva.BaseHtmlActicity;
import com.ciwong.epaper.modules.cordva.SubmitEvent;
import com.ciwong.epaper.modules.me.bean.JumpServiceParamBean;
import com.ciwong.epaper.modules.me.util.MeJumpManager;
import com.ciwong.epaper.util.HttpRequest;
import com.ciwong.epaper.util.IntentFlag;
import com.ciwong.epaper.util.RequestCode;
import com.ciwong.eventbus.EventBus;
import com.ciwong.libs.utils.CWLog;
import com.ciwong.libs.utils.volley.BaseRequest;
import com.google.gson.Gson;

/**
 * Created by paul on 2020/6/8.
 */

public class EssayRecordDetailH5Activity extends BaseHtmlActicity {

    private static final String TAG = "EssayRecordDetailH5Activity";
    private String title, doWorkId;
    Gson gson = new Gson();

    @Override
    protected void preCreate() {
        super.preCreate();
        Intent intent = getIntent();
        title = intent.getStringExtra(IntentFlag.INTENT_FLAG_TITLE);
        doWorkId = intent.getStringExtra(IntentFlag.INTENT_FLAG_OBJ);
    }

    @Override
    protected void init() {
        super.init();
        setValideSource(false);
        BaseRequest.VerifyInfo verifyInfo = HttpRequest.getVerifyInfo();
        if (verifyInfo != null) {
            String clientId = verifyInfo.getClientId();
            String accessToken = verifyInfo.getAccessToken();
            String brandId = EApplication.BRAND_ID + "";
            String startURL = EpaperConstant.URL_H5_ESSAY_DETAIL + "clientId=" + clientId + "&accessToken=" + accessToken + "&brandId=" + brandId + "&doWorkId=" + doWorkId;
            setStartURL(startURL);
            loadUrl(startURL.toString());
        } else {
            showToastError("无法获取用户信息，请重新登录后重试");
            finish();
        }
    }


    @Override
    protected void loadData() {
    }

    @Override
    protected void initEvent() {
        super.initEvent();
    }

    @Override
    protected void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }


    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void finish() {
        if (webView.canGoBack()) {
            webView.goBack();
            return;
        }
        super.finish();
    }


    public void onEventMainThread(SubmitEvent submitEvent) {
        CWLog.d(TAG, "onEventMainThread+" + submitEvent.getId() + "--Json:" + submitEvent.getJson());
        if (submitEvent.getId().equals("JumpServiceDetails")) {//
            JumpServiceParamBean jumpServiceParaBean = gson.fromJson(submitEvent.getJson(), JumpServiceParamBean.class);
            int serviceId = jumpServiceParaBean.getServiceId();
            String urlH5 = jumpServiceParaBean.getUrl();
            if (urlH5 == null || urlH5.equals(""))//当地址不存在时就跳到服务页面
                jumpToBookOrServerDetails(serviceId);
            else
                jumpToInnerResouce(urlH5);
        }
    }

    /**
     * 跳入h5内部资源
     *
     * @param urlH5
     */
    public void jumpToInnerResouce(String urlH5) {
        if (!urlH5.contains("http")) {//解决连接中不包含http 导致闪退的bug
            urlH5 = "http://" + urlH5;
        }
        MeJumpManager.jumpToResourseCenterInnerH5Activity(R.string.go_back, this, urlH5);

    }

    /**
     * 调到服务详情（购买页）
     */
    public void jumpToBookOrServerDetails(int serviceId) {
        String checkValidMsg = null;
        MeJumpManager.jumpBuyService(this, R.string.go_back, serviceId, -1, RequestCode.MainActivityResquestCode.REQUEST_CODE_TO_PAY, checkValidMsg);
    }
}
